package com.neudu.yiyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.yiyang.commons.JSONReturn;
import com.neudu.yiyang.pojo.Nursecontent;
import com.neudu.yiyang.pojo.Nurselevel;
import com.neudu.yiyang.service.NursecontentService;
import com.neudu.yiyang.service.NurselevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class NursecontentController {
    @Autowired
    private NursecontentService nursecontentService;
    @Autowired
    private NurselevelService nurselevelService;
    @Autowired
    private JSONReturn jsonReturn;

    @RequestMapping("get_all_nursing_projects")
    public String getAll(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        try {
            System.out.println("后端接收到的参数: pageNum = " + pageNum + ", pageSize = " + pageSize); // 添加日志输出
            PageHelper.startPage(pageNum, pageSize);
            List<Nursecontent> list = nursecontentService.list();
            PageInfo<Nursecontent> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }@RequestMapping("/get_all_nurse_levels")
    public String getAllNurseLevels() {
        try {
            LambdaQueryWrapper<Nurselevel> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Nurselevel::getIsDeleted, 0); // 过滤已删除的级别
            List<Nurselevel> levelList = nurselevelService.list(queryWrapper);
            return jsonReturn.returnSuccess(levelList);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError("获取护理级别失败：" + e.getMessage());
        }
    }

    @RequestMapping("/get_nursing_projects_by")
    public String getByCondition(
            Nursecontent nursecontent,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam(value = "level", required = false) String level // 直接使用护理级别名称查询
    ) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            LambdaQueryWrapper<Nursecontent> queryWrapper = new LambdaQueryWrapper<>();

            // 1. 处理护理级别查询（直接匹配nursecontent表的level_id字段）
            if (!ObjectUtils.isEmpty(level)) {
                queryWrapper.eq(Nursecontent::getLeveId, level); // 直接根据level_id字段查询
            }

            // 2. 处理原有查询条件（编号、名称、价格等）
            if (!ObjectUtils.isEmpty(nursecontent.getSerialNumber())) {
                queryWrapper.eq(Nursecontent::getSerialNumber, nursecontent.getSerialNumber());
            }
            if (!ObjectUtils.isEmpty(nursecontent.getNursingName())) {
                queryWrapper.eq(Nursecontent::getNursingName, nursecontent.getNursingName());
            }
            if (!ObjectUtils.isEmpty(nursecontent.getServicePrice())) {
                queryWrapper.eq(Nursecontent::getServicePrice, nursecontent.getServicePrice());
            }
            if (!ObjectUtils.isEmpty(nursecontent.getMessage())) {
                queryWrapper.like(Nursecontent::getMessage, nursecontent.getMessage());
            }
            if (!ObjectUtils.isEmpty(nursecontent.getStatus())) {
                queryWrapper.eq(Nursecontent::getStatus, nursecontent.getStatus());
            }

            List<Nursecontent> resultList = nursecontentService.list(queryWrapper);
            PageInfo<Nursecontent> pageInfo = new PageInfo<>(resultList);
            return jsonReturn.returnSuccess(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError("查询失败：" + e.getMessage());
        }
    }

    @RequestMapping("add_one_nursing_project")
    public String addOne(@RequestBody Nursecontent nursecontent) {
        try {
            if (ObjectUtils.isEmpty(nursecontent.getSerialNumber())) {
                return jsonReturn.returnError("插入失败，编号 不能为空");
            }
            nursecontent.setIsDeleted(0);
            nursecontentService.save(nursecontent);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_nursing_project_by_id")
    public String getById(@RequestParam("id") Integer id) {
        try {
            if (ObjectUtils.isEmpty(id)) {
                return jsonReturn.returnError("ID不能为空");
            }
            Nursecontent nursecontent = nursecontentService.getById(id);
            return jsonReturn.returnSuccess(nursecontent);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("modify_nursing_project_by_id")
    public String modifyById(@RequestBody Nursecontent nursecontent) {
        try {
            if (ObjectUtils.isEmpty(nursecontent.getId())) {
                return jsonReturn.returnError("更新失败，id 不能为空");
            }
            LambdaQueryWrapper<Nursecontent> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Nursecontent::getId, nursecontent.getId())
                    .eq(Nursecontent::getIsDeleted, 0);
            Nursecontent existingNursecontent = nursecontentService.getOne(queryWrapper);
            if (existingNursecontent == null) {
                return jsonReturn.returnError("更新失败，未找到对应的护理项目信息");
            }
            boolean success = nursecontentService.updateById(nursecontent);
            if (success) {
                return jsonReturn.returnSuccess(nursecontent);
            } else {
                return jsonReturn.returnError("更新失败，未找到对应的护理项目信息");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("toggle_nursing_project_status")
    public String toggleStatus(@RequestParam("id") Integer id) {
        try {
            if (ObjectUtils.isEmpty(id)) {
                return jsonReturn.returnError("ID不能为空");
            }

            // 1. 获取当前护理项目
            Nursecontent nursecontent = nursecontentService.getById(id);
            if (nursecontent == null) {
                return jsonReturn.returnError("未找到护理项目");
            }

            // 2. 切换状态
            Integer newStatus = (nursecontent.getStatus() == 1) ? 2 : 1;
            nursecontent.setStatus(newStatus);

            // 3. 更新数据库
            boolean success = nursecontentService.updateById(nursecontent);
            if (success) {
                return jsonReturn.returnSuccess("状态切换成功");
            } else {
                return jsonReturn.returnError("状态更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError("操作失败：" + e.getMessage());
        }
    }
}